import { Component, OnInit } from '@angular/core';
import {Router} from "@angular/router";
import {config} from '../../shared/smartadmin.config';
import { JsonApiService } from "app/core/api/json-api.service";
declare var $:any;
@Component({
	selector: 'app-algorithmCalibration',
	templateUrl: './algorithmCalibration.component.html',
	styleUrls: ['./algorithmCalibration.component.css']
})
export class AlgorithmCalibrationComponent implements OnInit{
	ACH16:any = [];//16通道
	loadometerData = [];
	calibrationType:string = '0101';//标定方式
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	carNumber:string = '';
	deviceId:string = '';
	weightCount:string|number = '6';
	beforeTime = '1';
	afterTime = '0';
	resultExplain = "";
	
	currentCoef = [];
	startTime:string = '';//table时间控件
	endTime:string = '';//table时间控件
	startTime_chart:string = '';//ehart折线图时间控件
	endTime_chart:string = '';//ehart折线图时间控件
	selectedRow:any;//选中的row数据
	isChartView = false;
	isMapView = false;
	drawTrackArr:any;//画轨迹的数据
	versionNumber=true;//版本号
	versionMessage = {
		carNumber:'未选择车辆',
		name:'未选择车辆',
		versionString:'未选择车辆',
		runStatus:'未选择车辆',
	};//版本信息
	specifyId = {
		picker1:'datetimepicker3',
		picker2:'datetimepicker4'
	};
	specifyTime = {
		startTime:'',
		endTime:'',
	};
	
	constructor(private router: Router,private jsonApiService: JsonApiService) {}
	ngOnInit() {
		this.initCheckBox();
	}
	
	initCheckBox(){
		this.ACH16=[];
		for(let i = 0;i < 16;i++){
			let itmes={isChecked:false,lable:'channel'+(i+1),value:''};
			this.ACH16.push(itmes);
		}
	}
	//获取设备通道状态
	resetCheckStatus(){
		let reqdata = 'deviceId=' + this.deviceId;
		this.jsonApiService.http("sensormonitor/getDeviceSensorById.html", reqdata).subscribe(result => {
			let data = result.data;
			if(data.sensorStatus){
				for (let i = 0;i < data.sensorStatus.length; i++){
					let _item = data.sensorStatus[i];
					if(_item){
						this.ACH16[i].isChecked = false;
					}else{
						this.ACH16[i].isChecked = true;
					}
				}
			}
		})
	}
	//获取设备信息
	getDeviceInfo(){
		let reqdata = "deviceId="+this.deviceId;
		this.jsonApiService.http("car/getCarInfoByDeviceId.html", reqdata).subscribe(result => {
			let data = result.data;
			if(data){
				this.calibrationType = data.useTypeL2?data.useTypeL2:'0101';
			}
		})
	}
	goBack(){
		window.history.go(-1);
	}
	//系数计算
	calculation(){
		let type:number = 1,sensorStatus:any = [];
		let channelFlag:boolean = false;
		if(this.calibrationType == '0102'){
			type = 2;
		}else{
			type = 1;
		}
		
		for(let item of this.ACH16){
			if(item.isChecked){
				channelFlag = true;
				sensorStatus.push(1)
			}else{
				sensorStatus.push(0)
			}
		}
		if(!channelFlag){
			this.jsonApiService.message('请选择需要标定的AD通道');
			return;
		}
		let reqdata = "type="+type+'&carNumber='+this.carNumber+'&deviceId='+this.deviceId+'&weightNum='+this.weightCount+
		'&beforeTime='+this.beforeTime+'&afterTime='+this.afterTime+'&sensorStatus='+sensorStatus;
		this.jsonApiService.http2("deviceCoefHandler/calculateCoef.html", reqdata).subscribe(result => {
			if(result.success){
				for(let item of this.ACH16){
					item.value = '';
					this.resultExplain = '';
				}
				let data = result.data;
				if(data.adArr.length == 0){
					this.jsonApiService.message(data.msg);
				}else{
					this.resultExplain = data.msg+'\n';
					for(var i = 0; i < data.adArr.length; i++){
						this.ACH16[i].value = data.adArr[i];
						this.resultExplain += data.adArr[i]+','
					}
					this.resultExplain = this.resultExplain.substring(0,this.resultExplain.lastIndexOf(","));
				}
			}else{
				this.jsonApiService.message(result.message);
			}
		})
	}
	//获取榜单数据
	weightRecord(modal){
		modal.show();
		this.loadometerData = [];
		let reqdata = "deviceId="+this.deviceId+'&count='+this.weightCount;
		this.jsonApiService.http("weighbridge/listWeighBridge.html", reqdata).subscribe(result => {
			if(result.success){
				this.loadometerData = result.data;
			}else{
				this.jsonApiService.message(result.message);
			}
		})
	}
	//系数下发
	factorSetQueue(){
		let channelFlag:boolean = false;
		let reqdata = "deviceId="+this.deviceId;
		for(let item of this.ACH16){
			item.value = item.value?item.value:'';
			if(item.value){
				channelFlag = true;
			}
			reqdata += '&'+item.lable+'='+item.value;
		}
		if(!channelFlag){
			this.jsonApiService.message('通道值不允许全部没有值');
			return;
		}
		this.jsonApiService.http("deviceCoefHandler/addDeviceCoefHandler.html", reqdata).subscribe(result => {
			this.jsonApiService.message(result.message);
		})
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
		this.versionMessage.carNumber = data.carNumber;
		this.initCheckBox();
		if(data.deviceId){
			this.getConfigParams();
			this.resetCheckStatus();
			this.getDeviceInfo();
		}else{
			this.calibrationType = '0101';
			this.versionMessage = {
				carNumber:'未选择车辆',
				name:'',
				versionString:'',
				runStatus:'',
			}
		}
		
	}
	jumpFactorQueue(){
		this.router.navigate(['/algorithm/factor-queue']);
		setTimeout(function(){
			$('#left-panel').find('.top-menu-invisible.open').find('.UL').css('display','none');
			$('#left-panel').find('.top-menu-invisible.open').removeClass('open');
			$('#left-panel').find('.top-menu-invisible.active').addClass('open');
			$('#left-panel').find('.top-menu-invisible.active').find('.UL').css('display','block');
		},300)
	}
	
	// 绘图,弹出框
	draw(drawModal){
		if(!this.deviceId){
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		drawModal.show(0);
		const emptyLoadToS = common.mmsstoTimestamp(common.getNowTime());
		this.endTime_chart = common.getNowTime();
		this.startTime_chart = common.getTimeMmss(new Date(emptyLoadToS-1*24*60*60*1000));;
		this.specifyTime = {
			"startTime":this.startTime_chart, 
			"endTime":this.endTime_chart
		}
		let params = {
			deviceId:this.deviceId,
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
			coefficient:this.currentCoef
		}
		this.selectedRow = params;
		this.drawAbnormalChart();
			
	}
	
	showMapView(val){
		this.isMapView = val;
	}
	// 关闭图标
	closeChart():void{
		this.isChartView = false;
	}
	//折线图时间回调事件
	getSelectChartTime(time){
		this.startTime_chart = time.startTime;
		this.endTime_chart = time.endTime;
	}
	//数据查看
	drawAbnormalChart(){
		let params = {
			deviceId:this.deviceId,
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
			coefficient:this.currentCoef
		}
		this.selectedRow = params;
	}
	
	runParent(value){
	   this.drawTrackArr = value;
	}
	// 获取系数
	getConfigParams(){
		// if(!this.versionNumber) return;
		if(!this.deviceId ) return;
		let reqdata = 'deviceId=' + this.deviceId;
		this.jsonApiService.http("realtime/getRealTimeData.html", reqdata).subscribe(result => {
			this.currentCoef = result.data.coef;
			this.versionMessage.name=result.data.version;
			this.versionMessage.runStatus=result.data.runStatus;
			
			let coefArr=[];
			if(!result.data.coef) return
			for (let i=0;i<result.data.coef.length;i++){
				let item=result.data.coef[i];
				if(item){
					let vname='通道'+(i+1);
					coefArr.push(vname+':'+item);
				}
			}
			let resultString='';
			for(let _i=0;_i<coefArr.length;_i++){
				if(_i<coefArr.length-1){
					resultString+=coefArr[_i]+',';
				}else{
					resultString+=coefArr[_i]
				}
			}
			this.versionMessage.versionString=resultString;
		})
	}
}
